<script setup lang="ts">
import { loginApi } from '@/apis/user';
import { useUserStore } from '@/stores/userStore';
import type { LoginParams } from '@/types/user';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

// 表单数据
const formData = ref<LoginParams>({
  username: 'demo',
  password: 'Hmzs%001'
});
// 表单校验规则
const formRules = ref<FormRules<LoginParams>>({
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
});
// 表单实例
const formRef = ref<FormInstance>();

const userStore = useUserStore();

const router = useRouter();

// 添加记住我的状态
const rememberMe = ref(false)

// 组件挂载时检查是否有保存的登录信息
onMounted(() => {
  const savedLoginInfo = localStorage.getItem('loginInfo')
  if (savedLoginInfo) {
    const { username, password, remember } = JSON.parse(savedLoginInfo)
    if (remember) {
      formData.value.username = username
      formData.value.password = password
      rememberMe.value = true
    }
  }
})

// 登录
const onSubmit = () => {
  // 表单校验
  formRef.value?.validate(async (valid) => {
    if (valid) {
      // 保存或清除登录信息
      if (rememberMe.value) {
        localStorage.setItem('loginInfo', JSON.stringify({
          username: formData.value.username,
          password: formData.value.password,
          remember: true
        }))
      } else {
        localStorage.removeItem('loginInfo')
      }

      // 登录
      const res = await loginApi(formData.value)
      console.log('res',res.data.token)
      // console.log('登录成功');
      userStore.setToken(res.data.token)
      ElMessage.success('登录成功');
      router.push('/')
    }else {
      ElMessage.error('请输入账号和密码');
    }
  });
};

</script>

<template>
  <div class="login_body">
    <div class="bg" />
    <div class="box">
      <div class="title">智慧园区-登录</div>
      <el-form ref="formRef" :model="formData" :rules="formRules">
        <el-form-item label="账号" prop="username">
          <el-input v-model="formData.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="formData.password" type="password" />
        </el-form-item>

        <el-form-item prop="remember">
          <el-checkbox v-model="rememberMe">记住我</el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="login_btn" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login_body {
  display: flex;
}

.bg {
  width: 60vw;
  height: 100vh;
  background: url("@/assets/login-bg.svg") no-repeat;
  background-position: right top;
  background-size: cover;
}

.box {
  margin: 200px 10% 0;
  flex: 1;

  .title {
    padding-bottom: 76px;
    font-size: 26px;
    font-weight: 500;
    color: #1e2023;
  }

  ::v-deep() {
    .ant-form-item {
      display: flex;
      margin-bottom: 62px;
      flex-direction: column;
    }

    .ant-form-item-label label {
      font-size: 16px;
      color: #8b929d;
    }

    .ant-input,
    .ant-input-password {
      border-radius: 8px;
    }
  }
}

.login_btn {
  width: 100%;
}
</style>
